package com.kun.portal.webframe.service;

import com.kun.portal.webframe.dao.FlightPlaneDao;
import com.kun.portal.webframe.po.FlightPlanePo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018-07-24.
 * 航班动态
 */
@Service
public class FlightDynamicService {
    @Autowired
    private FlightPlaneDao flightPlaneDao;

    public List<FlightPlanePo> query(Date expectDate, String importOrExport){
        Specification query = new Specification<FlightPlanePo>() {
            @Override
            public javax.persistence.criteria.Predicate toPredicate(Root<FlightPlanePo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<javax.persistence.criteria.Predicate> pre= new ArrayList<>();
                if(null != expectDate){
                    pre.add(cb.equal(root.get("expectDate"), expectDate));
                }
                if(StringUtils.isNotBlank(importOrExport)){
                    pre.add(cb.like(root.get("importOrExport"), "%"+importOrExport+"%"));
                }
                Predicate predicate = cb.and(pre.toArray(new Predicate[pre.size()]));
                return predicate;
            }
        };
        return flightPlaneDao.findAll(query);
    }

}
